package com.chinaisafe.user.biz.impl;

import com.chinaisafe.base.common.exception.BizException;
import com.chinaisafe.user.biz.SysMenuBiz;
import com.chinaisafe.user.entity.SysApp;
import com.chinaisafe.user.entity.SysMenu;
import com.chinaisafe.user.entity.SysModule;
import com.chinaisafe.user.service.SysAppService;
import com.chinaisafe.user.service.SysMenuService;
import com.chinaisafe.user.service.SysModuleService;
import com.chinaisafe.user.vo.request.SysMenuReqVO;
import com.chinaisafe.user.vo.response.SysMenuRespVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * SysMenuBiz
 * Description：菜单业务
 *
 * @author tby
 * &#064;date  2024/7/13 9:44
 */
@Service
public class SysMenuBizImpl implements SysMenuBiz {

    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private SysAppService sysAppService;
    @Resource
    private SysModuleService sysModuleService;

    @Override
    public SysMenuRespVO getDetailById(Long sysMenuId) {
        SysMenu sysMenu = sysMenuService.getById(sysMenuId);
        Optional.ofNullable(sysMenu).orElseThrow(() -> BizException.build("未找到对应菜单(资源)信息"));
        SysApp sysApp = sysAppService.getById(sysMenu.getSysAppId());
        SysModule sysModule = sysModuleService.getById(sysMenu.getSysModuleId());
        return SysMenuRespVO.convertFromEntity(sysMenu, sysApp, sysModule);

    }

    @Override
    public void enableSwitch(Long id) {
        SysMenu sysMenu = sysMenuService.getById(id);
        if (sysMenu == null) {
            throw BizException.build("未找到对应菜单(资源)信息");
        }
        if (sysMenu.getEnable()) {
            // 禁用
            disableMenu(sysMenu);
        } else {
            // 启用
            enableMenu(sysMenu);
        }

    }

    @Override
    public SysMenuRespVO update(SysMenuReqVO sysMenuReqVO) {

        // 编辑
        SysMenu sysMenu = sysMenuService.getById(sysMenuReqVO.getId());
        Optional.ofNullable(sysMenu).orElseThrow(() -> BizException.build("未找到要修改的菜单(资源)信息"));
        if (sysMenuReqVO.getSysAppId() != null && !sysMenu.getSysAppId().equals(sysMenuReqVO.getSysAppId())) {
            throw BizException.build("菜单所属应用不能修改");
        }
        if (sysMenuReqVO.getSysModuleId() != null && !sysMenu.getSysModuleId().equals(sysMenuReqVO.getSysModuleId())) {
            throw BizException.build("菜单所属模块不能修改");
        }

        SysMenu sysMenu2Save = sysMenuReqVO.convertToEntity();
        boolean success = sysMenuService.saveOrUpdate(sysMenu2Save);
        if (success) {
            return SysMenuRespVO.convertFromEntity(sysMenu2Save, null, null);
        } else {
            BizException.throwException("操作失败，请刷新后重试");
        }
        return null;
    }

    private void enableMenu(SysMenu sysMenu) {
        SysMenu parent = sysMenuService.lambdaQuery().eq(SysMenu::getId, sysMenu.getParentId()).one();
        if (parent != null && !parent.getEnable()) {
            throw BizException.build("请先启用父级菜单");
        }
        sysMenuService.lambdaUpdate().eq(SysMenu::getId, sysMenu).set(SysMenu::getEnable, true).update();
    }

    private void disableMenu(SysMenu id) {
        Long childCount = sysMenuService.lambdaQuery().eq(SysMenu::getParentId, id).eq(SysMenu::getEnable, true).count();
        if (childCount > 0) {
            throw BizException.build("请先禁用全部子菜单");
        }
        sysMenuService.lambdaUpdate().eq(SysMenu::getId, id).set(SysMenu::getEnable, false).update();
    }
}
